package mammuthus.template

import mammuthus.tool.TemplateTool
import mammuthus.slave.ShellCommand
import net.csdn.common.logging.Loggers
import mammuthus.protocal.DockerInstanceDetail

/**
 * 5/7/15 WilliamZhu(allwefantasy@gmail.com)
 */
trait DockerCommand {
  val tlogger = Loggers.getLogger(classOf[DockerCommand])
  def dockerCommandRender(name: String) = {
    TemplateTool.render(s"/template/command/docker/${name}.vm", Map())
  }

  def dockerCommandRender(name: String, params: Map[String, AnyRef]) = {
    TemplateTool.render(s"/template/command/docker/${name}.vm", params)
  }

  def dockerVersion = {
    ShellCommand.execWithExitValue("docker version")
  }

  def dockerInstanceIds = {
    val command = dockerCommandRender("dockerInstanceIds", Map("showAl" -> "false"))
    val res = ShellCommand.execWithExitValue(command)._3.split("\n").toList
    tlogger.info(res.mkString(","))
    res
  }

  def dockerIp(instanceId:String) = {
    val command = dockerCommandRender("dockerIP", Map("instanceId" -> instanceId))
    val res = ShellCommand.execWithExitValue(command)._3
    res
  }

  def isDockerRegistry(instanceId:String) = {
    val command = dockerCommandRender("dockerRegistry", Map("instanceId" -> instanceId))
    ShellCommand.execWithExitValue(command)._1 == 0
  }

}
